package com.zbl.zblproject.core.component;

import com.zbl.zblproject.core.entity.GoodsLog;
import com.zbl.zblproject.core.mapper.GoodsLogMapper;
import com.zbl.zblproject.erp.entity.ErpGoods;
import com.zbl.zblproject.erp.mapper.ErpGoodMapper;
import com.zbl.zblproject.erp.mapper.ErpTempOrderItemMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/12/5
 */
@Component
@EnableScheduling
public class TaskComponent {

    private static final Logger logger = LoggerFactory.getLogger(TaskComponent.class);

    @Autowired
    private ErpGoodMapper goodsMapper;

    @Autowired
    private GoodsLogMapper goodsLogMapper;

    @Autowired
    private ErpTempOrderItemMapper erpTempOrderItemMapper;


    /**
     * 每天九点保存信息
     * id,用戶名称，营业额，利润，利率，创建时间，状态，报表类型
     */
//   @Scheduled(cron = "0 59 20 * * ?")
//   public void speakSomethings(){
//       //todo 报表
//    }

    /**
     * 备份数据库中有库存的商品数据
     */
    @Scheduled(cron = "0 59 20 1 * ?")
    public void saveGoods(){
        LocalDateTime now = LocalDateTime.now();
        List<ErpGoods> list = goodsMapper.selectList(null);
        if(!list.isEmpty()){
            for(ErpGoods goods : list){
                GoodsLog log = new GoodsLog();
                log.setGoodsId(goods.getGoodsId());
                log.setGoodsStock(goods.getGoodsNumber());
                log.setGoodsPrice(goods.getShopPrice());
                log.setDate(now);
                //计算本月数据
                log.setMonthAmount(erpTempOrderItemMapper.countGoodsAmountOfTheBeforeMonth(goods.getGoodsId()));
                goodsLogMapper.insert(log);
            }
        }
        logger.info("执行 cron");
    }


}
